package ModelLayer;

import java.util.*;
public class CustomerContainer
{
    private ArrayList<Customer>  customerContainer;
    private static CustomerContainer instance;

    private CustomerContainer()
    {
        customerContainer = new  ArrayList<Customer>();
    }

    public static CustomerContainer getInstance(){
        if (instance == null){
            instance = new CustomerContainer();
        }
        return instance;
    }

    public Customer findCustomerByName(String name){
        Customer pObject = null;
        int index = 0;
        boolean found = false;
        while(index < customerContainer.size() && !found)
        {
            pObject = customerContainer.get(index); 
            if(pObject.getName().equals(name))
            {
                found = true;
            }
            else{
                index++;
            }
        }
       
           if(found){
           return customerContainer.get(index) ;
        }else{
            return  null;
        }
    }
    
    public Customer findCustomerById(int id){
        Customer pObject = null;
        int index = 0;
        boolean found = false;
        while(index < customerContainer.size() && !found)
        {
            pObject = customerContainer.get(index); 
            if(pObject.getId() == id)
            {
                found = true;
            }
            else{
                index++;
            }
        }
      
           if(found){
           return customerContainer.get(index) ;
        }else{
            return  null;
        }
    }
    
    public boolean checkCustomer(int customerID)
    {
        boolean ok = true;
        Customer customer = findCustomerById(customerID);
        if(customer == null){
            ok = false;
        }

        return ok;
    }
    

    public void addCustomer(Customer p){
        customerContainer.add(p);
    }

    public void deleteCustomer(Customer p){
        if(customerContainer.contains(p))
            customerContainer.remove(p);
    }
    
    public ArrayList<Customer> getCustomersList()
    {
        return customerContainer;
    }
}
